﻿import os
import string
import time
import datetime
import socket
import sys
import types
import re
import copy
import configparser 
class IniConfig:
    def __init__(self, config_file_path):
        cf = configparser.ConfigParser()  
        cf.read(config_file_path)  
        self.ftp_ip = cf.get("FTP_PARM", "ADDRESS")
        self.ftp_port = cf.getint("FTP_PARM", "PORT")
        self.dbs1_ip = cf.get("DBS1_1", "DBS_HOST")
        self.dbs1_port = 4698
        self.dbs2_ip = cf.get("DBS1_1", "DBS_HOST")
        self.dbs2_port = 4698
        self.ads_ip = cf.get("ADS_PARM","ADDRESS")
        self.ads_port = cf.get("ADS_PARM","PORT")
    #ftp    
    def getFTPIp(self):
        return self.ftp_ip;
    def getFTPPort(self):
        return self.ftp_port;
    #sds1
    def getDBS1Ip(self):
        return self.dbs1_ip;
    def getDBS1Port(self):
        return self.dbs1_port;
    #sds2
    def getDBS2Ip(self):
        return self.dbs2_ip;
    def getDBS2Port(self):
        return self.dbs2_port;
    #ads
    def getADSIp(self):
        return self.ads_ip;
    def getADSPort(self):
        return self.ads_port;
 

def check(pypath,timeout):
    return 0
    f = IniConfig(pypath)
    result = connect(f.getFTPIp(),f.getFTPPort(),timeout)
    if result != 0:
        return 1
    result = connect(f.getDBS1Ip(),f.getDBS1Port(),timeout)
    if result != 0:
        return 2
    result = connect(f.getDBS2Ip(),f.getDBS2Port(),timeout)
    if result != 0:
        return 3
    result = connect(f.getADSIp(),f.getADSPort(),timeout)
    if result != 0:
        return 4
    return 0
   
def connect(ip,port,timeout):
    try:
        cs=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
        address=(str(ip),int(port))
        status = cs.connect_ex((address))
        cs.settimeout(timeout)
        if status != 0 :
            return 1	
        else:
            return 0	
    except : 
        return 1
    return 0 

def monitor():
    result = os.popen("sc query AdCaster")
    if not "RUNNING" in result:
        os.popen("sc start AdCaster")

if __name__=='__main__':
    ret = 0
	#check("E:\\AdCenter\\SRC\\V300\\trunk\\AdCastServer\\AdCaster\\config\\app.ini",5)
    print ("result", ret) 
 
